﻿var Try = {
  these: function() {
    var returnValue;

    for (var i = 0, length = arguments.length; i < length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) { }
    }

    return returnValue;
  }
};

function ajax(url, type, param)
{
	var isCache = false;// false:no-cache or true:cache
	
	http_request = Try.these(
      function() {return new XMLHttpRequest()},
      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
      function() {return new ActiveXObject('Microsoft.XMLHTTP')}
    ) || false;

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	if( !isCache )
	{
		var t = new Date().getTime();
		if (url.indexOf("?") > 0) {
			url += "&t=" + t;
		} else {
			url += "?t=" + t;
		}
	}
		
	http_request.onreadystatechange = handleStateChange;// 回调函数
	http_request.open("GET", url, true);
	http_request.send(null);

	function handleStateChange(){
		if(http_request.readyState==4){
			if(http_request.status==200) {// 成功加载
				switch (type) {
					case "update" :
						$(param).innerHTML = http_request.responseText;
						break;
					case "script" :
						eval(http_request.responseText);
						break;
				}
			}
		}
	}
}

function AjaxCall(url, handleResponse, param) {
	var isXML = false;// false:html or true:xml
	var isCache = false;// false:no-cache or true:cache
	var method = "GET";// GET or POST
	if (param) {
		re = / /g;
		param = param.replace(re, "");
		var tmp = param.split(",");// extends param array
		for (var i = 0; i < tmp.length; i++) {
			var pa = tmp[i].substr(0, 3);
			var ct = tmp[i].split("=")[1];
			switch (pa) {
				case "isX" :
					if (ct == "true") {
						isXML = true;
					} else {
						isXML = false;
					}
					break;
				case "isC" :
					if (ct == "true") {
						isCache = true;
					} else {
						isCache = false;
					}
					break;
				case "met" :
					method = ct;
					break;
			}
		}
	}
	if (!isCache) {
		var t = new Date().getTime();
		if (url.indexOf("?") > 0) {
			url += "&t=" + t;
		} else {
			url += "?t=" + t;
		}
	}
	var postdata = null;
	if (method == "POST") {
		purl = url.split("?");
		url = purl[0];
		postdata = purl[1];// must urlencoded first
	}
	RequestHTTP(url, handleResponse, method, isXML, postdata);
}

function AJAXForm(formID, handleResponse, gXML) {
	var isXML = false;// false:html or true:xml
	if (gXML) {
		re = / /g;
		gXML = gXML.replace(re, "");
		var ct = gXML.split("=")[1];
		if (ct == "true") {
			isXML = true;
		} else {
			isXML = false;
		}
	}

	var url = $(formID).action;
	var data = getForm(formID);
	RequestHTTP(url, handleResponse, "POST", isXML, data);
}

function getForm(theFormName) {
	var form = document.forms[theFormName];
	if (typeof form.name == "undefined" || form.name == "")
		form = $(theFormName);

	var formData = "";
	var element;
	for (var i = 0; i < form.elements.length; i++) {
		element = form.elements[i];
		var type = element.type.toLowerCase();
		if (type == "hidden" || type == "text" || type == "password"
				|| type == "textarea") {
			formData += element.name + "=" + $U(element.value) + "&";
		} else if (element.type.indexOf("select") != -1) {
			for (var j = 0; j < element.options.length; j++) {
				if (element.options[j].selected == true)
					formData += element.name + "="
							+ $U(element.options[element.selectedIndex].value)
							+ "&";
			}
		} else if (element.type == "checkbox") {
			if (element.checked)
				formData += element.name + "=" + $U(element.value) + "&";
		} else if (element.type == "radio") {
			if (element.checked == true)
				formData += element.name + "=" + $U(element.value) + "&";
		}
	}
	return formData.substring(0, formData.length - 1);
}

var http_request = false;

function RequestHTTP(burl, rdRandleResponse, bmethod, bXML, Formdata) {
	function doHttpRequest() {
		http_request = Try.these(
	      function() {return new XMLHttpRequest()},
	      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
	      function() {return new ActiveXObject('Microsoft.XMLHTTP')}
	    ) || false;
    
		if (!http_request) {
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}

		http_request.onreadystatechange = handleStateChange;
		http_request.open(bmethod, burl, true);
		if (Formdata) {
			http_request.setRequestHeader("Content-Length", Formdata.length);
			http_request.setRequestHeader("Content-Type",
					"application/x-www-form-urlencoded; charset=utf-8");
			http_request.send(Formdata);
		} else {
			if (bXML) {
				http_request.setRequestHeader("Content-Type", "text/xml");
				// charset "text/xml;charset=utf-8";
			} else {
				http_request.setRequestHeader("Accept-Charset", "utf-8");
				http_request.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			}
			http_request.send(null);
		}

	}

	function handleStateChange() {
		if (http_request.readyState == 4 && http_request.status == 200) {
			var doc;
			if (bXML) {
				content = http_request.responseXML;
			} else {
				content = http_request.responseText;
			}
			
			eval(rdRandleResponse + "(content)");
		}
	}
	doHttpRequest();
}

// tools
function getTagValue(doc, tag) {
	var elems = doc.getElementsByTagName(tag)[0].firstChild.nodeValue;
	return elems;
}

function $(obj) {
	return document.getElementById(obj);
}

function $F(obj) {
	return document.getElementById(obj).value;
}
function $U(str) {
	return encodeURIComponent(escape(str));
}

function insertFlash(elm, url, w, h) 
{ 
	if (!document.getElementById(elm)) return; 
	
	var str = ''; 
	
	if ( url.indexOf('.flv') != -1 )
	{
		str += '<object width="'+ w +'" height="'+ h +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0">'; 
		str += '<param name="movie" value="/images/flash/vcastr22.swf">'; 
		str += '<param name="FlashVars" value="vcastr_file='+url+'&IsAutoPlay=1&IsContinue=1">';
		str += '<param name="wmode" value="transparent">'; 
		str += '<param name="menu" value="false">';
		str += '<param name="allowFullScreen" value="true">';
		str += '<param name="quality" value="autohigh">'; 
		str += '<param name="bgcolor" value="#ffffff">'; 
		str += '<embed bgcolor="#ffffff" width="'+ w +'" height="'+ h +'" src="/images/flash/vcastr22.swf" allowFullScreen="true" FlashVars="vcastr_file='+url+'&IsAutoPlay=1&IsContinue=1" menu="false" quality="autohigh" wmode="opaque" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>'; 
		str += '</object>'; 
	}
	else
	{
		str += '<object width="'+ w +'" height="'+ h +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0">'; 
		str += '<param name="movie" value="'+url+'">'; 
		str += '<param name="wmode" value="transparent">'; 
		str += '<param name="menu" value="false">';
		str += '<param name="allowFullScreen" value="true">';
		str += '<param name="quality" value="autohigh">'; 
		str += '<param name="bgcolor" value="#ffffff">'; 
		str += '<embed bgcolor="#ffffff" width="'+ w +'" height="'+ h +'" src="'+url+'" allowFullScreen="true" menu="false" quality="autohigh" wmode="opaque" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>'; 
		str += '</object>'; 
	}

	document.getElementById(elm).innerHTML = str; 
} 

String.prototype.trim= function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}  